
/* JavaScript content from js/PersonalDevelopmentApp.js in folder common */

function wlCommonInit(){
	// Common initialization code goes here
	loadThoughts(1);
}


/*
 * Load feeds & Display a feed item
 */
function loadThoughts(owner_id){
	WL.Logger.debug("Thoughts::load");
	jq.mobile.showPageLoadingMsg();
	var invocationData = {
			adapter: "DataControllerAdaptor",
			procedure: "listThoughts",
			parameters: [owner_id]
	};
	
	WL.Client.invokeProcedure(invocationData, {
		onSuccess: loadThoughtsSuccess, 
		onFailure: loadThoughtsFail
	});
}


function loadThoughtsSuccess(data){
	WL.Logger.debug("Thoughts::load successfully");
	if (!data || !data.invocationResult || !data.invocationResult.resultSet || data.invocationResult.resultSet.length==0)
		showErrorMessage("Could not retrieve correct data from server");
	
	var thoughts = data.invocationResult.resultSet;
	displayThoughts(thoughts);
}

function loadThoughtsFail(data){
	WL.Logger.debug("Thoughts::load failed");
	showErrorMessage("Server connectivity error");
}

function displayThoughts(thoughts){
	jq("#ThoughtsList").empty();
	
	var markup = "<li data-role='list-divider'> ${create_date} <span class='ui-li-count'>1</span></li>" +
			"<li><a href='#ThoughtContentPage'> <h3> </h3>" +
			"<p><strong>${thought}</strong></p> <p>${action}</p>" +
			"<p class='ui-li-aside'><strong>6:24</strong>PM</p></a></li>";
	// Compile the markup as a named template
	jq.template( "listTemplate", markup );
	// Render the template with the movies data
	jq.tmpl( "listTemplate", thoughts ).appendTo( "#ThoughtsList" );
	
	jq("#ThoughtsList").listview('refresh');
	jq.mobile.hidePageLoadingMsg();
}




function showErrorMessage(text){
	jq.mobile.hidePageLoadingMsg();
	jq("#DialogText").html(text);
	jq.mobile.changePage("#DialogPage");
}


/* JavaScript content from js/PersonalDevelopmentApp.js in folder android */
// This method is invoked after loading the main HTML and successful initialization of the Worklight runtime.
function wlEnvInit(){
    wlCommonInit();
    // Environment initialization code goes here
}